{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-4">
	<h3 class="pb-3">功能菜单/节点</h3>
	<table class="layui-table layui-table-form">

		<tr>
			<td class="layui-td-gray-2">销售单位<font>*</font>
			<td><select name="saleUnit" id="">
			{volist name="saleUnitList" id="sl"}
			<option value="{$sl.id}" {if isset($detail.saleUnit) &&$detail.saleUnit == $sl.id} selected{/if}>{$sl.title}</option>

			{/volist}
		</select></td>
		</tr>
		<tr>
			<td class="layui-td-gray-2">客户<font>*</font>
			</td>
			<td>
				<select name="purveyorId" id="purveyorId" lay-filter="purveyorId" lay-search>
					<option value="">请选择</option>
					{volist name="punList" id="pl"}
					<option value="{$pl.id}" {if isset($detail.purveyorId)&&$detail.purveyorId == $pl.id} selected {/if}>{$pl.name}</option>
					{/volist}
				</select>
			</td>
			<td class="layui-td-gray">单据日期<font>*</font>
			</td>
			<td>
				<input type="text" name="orderDate" id="orderDate" value="{$detail.orderDate|default=date('Y-m-d')}" lay-verify="required" readonly autocomplete="off" placeholder="请输入"
					   lay-reqText="请输入" class="layui-input">
			</td>


			<td class="layui-td-gray">收款方式<font>*</font>
			</td>
			<td>
				<select name="payType" >
					{foreach ['1'=>'现金','2'=>'银行电汇','3'=>'电子承兑'] as $pkey=>$pt}
					{if $id}
					<option value="{$pkey}" {if isset($detail.payType)&&$detail.payType == $pkey} selected {/if}>{$pt}</option>
					{else}
					<option value="{$pkey}" {if $pkey==2} selected {/if}>{$pt}</option>
					{/if}

					{/foreach}
				</select>
			</td>
		</tr>
<!--		<tr>-->
<!--			<td class="layui-td-gray">关联合同<font>*</font>-->
<!--			</td>-->
<!--			<td>-->
<!--				<select name="contractOrder" id="contractOrder" lay-filter="contractOrder" lay-search>-->
<!--					<option value="">选择客户</option>-->
<!--				</select>-->
<!--			</td>-->

<!--			</td>-->
<!--			<td class="layui-td-gray">未付金额 </td>-->
<!--			<td>-->
<!--				<input type="text" readonly class="layui-input" id="unPay">-->
<!--			</td>-->
<!--			<td class="layui-td-gray">合同金额 </td>-->
<!--			<td>-->
<!--				<input type="text" readonly name="contactMoney" class="layui-input" id="contactMoney" value="{$detail.contactMoney|default=0}">-->
<!--			</td>-->

<!--		</tr>-->
		<tr>
			<td class="layui-td-gray">金额<font>*</font>
			</td>
			<td>
				<input type="text" name="money" value="{$detail.money|default=''}" lay-verify="required" autocomplete="off" placeholder="请输入"
					   lay-reqText="请输入" onblur="digitalFiltering(this,2)" class="layui-input">
			</td>
			<td class="layui-td-gray">收款银行<font>*</font>
			<td>
			<select name="payBank" lay-search>
				{volist name="bankList" id="bl"}
				<option value="{$bl}" {if isset($detail.payBank)&&$detail.payBank == $bl} selected {/if}>{$bl}</option>
				{/volist}
			</select>
			</td>
			<td class="layui-td-gray">收款日期<font>*</font>
			</td>
			<td>
				<input type="text" name="payDate" id="payDate" value="{$detail.payDate|default=date('Y-m-d')}" lay-verify="required" readonly autocomplete="off" placeholder="请输入"
					   lay-reqText="请输入" class="layui-input">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">备注
			</td>
			<td colspan="5">
				<input type="text" name="moRemark" value="{$detail.moRemark|default=''}" autocomplete="off" placeholder="请输入"
					   lay-reqText="请输入"  class="layui-input">
			</td>
		</tr>
	</table>
	<div class="py-3">
		<input type="hidden" name="id" value="{$id}">
		{if !isset($detail.status)||$detail.status==1}
		<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">保存</button>
		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		{/if}
		{if isset($detail.status)&&$detail.status==1}
		<a href="javascript:" class="layui-btn layui-btn-normal" onclick="onSub()">确认</a>
		{/if}
		{if isset($detail.status)&&$detail.status==2}
		<a href="javascript:" class="layui-btn layui-btn-warm" onclick="onCancel()">撤销</a>
		{/if}
	</div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const moduleInit = ['tool'];
	let contractList=[]
	let contractOrder="{$detail.contractOrder|default=''}"
	let punterId="{$detail.purveyorId|default=''}"
	function gouguInit() {
		var form = layui.form,tool=layui.tool,level=0;

		form.on('select(purveyorId)', function(data){
			let val=data.value
			if (val==''){
				layer.msg('请选择客户');
			}
			getContract(val);
		})

		form.on('select(contractOrder)', function(data){
			let val=data.value
			console.log('aaaaa',val)
			checkContact(val)
		})
		//监听提交
		form.on('submit(webform)', function (data) {
			let callback = function (e) {
				layer.msg(e.msg);
				if (e.code == 0) {
					setTimeout(function(){
						parent.parent.location.reload();
					},1000);									
				}
			}
			tool.post("add", data.field, callback);
			return false;
		});
		layui.laydate.render({
			elem:"#orderDate"
		})
		layui.laydate.render({
			elem:"#payDate"
		})

		getContract(punterId);

	}

	function getContract(punterId='') {
		contractList=[];
		$("#contactMoney").val('0');
		$("#contractOrder").empty()
		if (!punterId){
			$("#contractOrder").empty()
			layui.form.render();
			return;
		}else {

			let param={
				type:1,
				punterId:punterId,
				isOver:1
			};
			httpRequest("{:url('api/getContractList')}",param,function (res) {
				console.log('res',res)
				if (res.code==0){
					let str='<option value="">选择合同</option>';
					let data=res.data;
					contractList=data;
					data.forEach(function (item) {
						let seText=item.orderNum==contractOrder?'selected':'';
						str+='<option value="'+item.orderNum+'" '+seText+'>'+item.orderNum+'</option>'
					})
					$("#contractOrder").append(str)
					layui.form.render();
				}else {
					$("#contractOrder").empty()
					layui.form.render();
				}
		checkContact(contractOrder)
			},false)

		}

	}

	function checkContact(contractOrder='') {
		let contactMoney=0;
		let unPay=0;
		if (contractOrder==''){
			$("#contactMoney").val(contactMoney);
			$("#unPay").val(unPay);
			return ;
		}
		console.log('ccccc')

		contractList.forEach(function (item) {
			console.log(item.orderNum==contractOrder)
			if (item.orderNum==contractOrder){
				contactMoney=item.countMoney;
				unPay=item.unPay;
			}
		})
		$("#contactMoney").val(contactMoney);
		$("#unPay").val(unPay);
	}

	function onSub() {
		let id="{$id}";
		if (id==0){
			layer.msg('操作有误');
			return;
		}
		httpRequest("{:url('onSure')}",{id:id},function (res) {
			if (res.code == 0) {
				setTimeout(function () {
					parent.parent.location.reload();
				}, 1000);
			}
		})
	}
	function onCancel() {
		let id="{$id}";
		if (id==0){
			layer.msg('操作有误');
			return;
		}
		httpRequest("{:url('onCancel')}",{id:id},function (res) {
			if (res.code == 0) {
				setTimeout(function () {
					parent.parent.location.reload();
				}, 1000);
			}
		})
	}
</script>
{/block}
<!-- /脚本 -->